//
//  BLHotelCommitViewController.m
//  blanket
//
//  Created by Mac on 2018/11/14.
//  Copyright © 2018 stoneobs@icloud.com. All rights reserved.
//

#import "BLHotelCommitViewController.h"
#import "BLTableControl.h"
#import "NSDate+STFormatter.h"
#import "NSCalendar+STCalendarTool.h"
#import "UIViewController+STKeybordAdjust.h"
#import "BLHotlePayViewController.h"
@interface BLHotelCommitViewController ()
@property(nonatomic, strong) BLTableControl                     *numControl;/**<  */
@property(nonatomic, strong) BLTableControl                     *dayControl;/**<  */
@property(nonatomic, strong) BLTableControl                     *nameControl;/**<  */
@property(nonatomic, strong) BLTableControl                     *phoneControl;/**<  */
@property(nonatomic, strong) BLTableControl                     *timeControl;/**<  */
@property(nonatomic, strong) STButton                     *moneyButton;/**< <##> */
@end

@implementation BLHotelCommitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"房间预订";
    [self configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    [self st_autoAdjustAllResponder];
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 0)];
    header.backgroundColor = UIColor.whiteColor;
    
    ({
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, header.height, UIScreenWidth - 30, 40)
                                                         text:[NSString stringWithFormat:@"酒店名称:%@",
                                                               self.hotelModel.hotel_name]
                                                    textColor:FirstTextColor
                                                         font:15
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [header addSubview:titleLable];
        header.height = titleLable.bottom;
    });
    
    ({
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, header.height, UIScreenWidth - 30, 40)
                                                         text:[NSString stringWithFormat:@"酒店地址:%@",self.hotelModel.address]
                                                    textColor:SecendTextColor
                                                         font:15
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [header addSubview:titleLable];
        header.height = titleLable.bottom;
    });
    ({
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, header.height, UIScreenWidth - 30, 40)
                                                         text:[NSString stringWithFormat:@"房间名称:%@",
                                                               self.detailModel.room_name]
                                                    textColor:SecendTextColor
                                                         font:15
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [header addSubview:titleLable];
        header.height = titleLable.bottom;
    });
    
    ({
        self.numControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + header.height, UIScreenWidth - 30, 50) leftTitle:@"房间数" placeHolder:@"请输入房间数量"];
        [header addSubview:self.numControl];
        header.height = self.numControl.bottom;
        self.numControl.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    });
    
    ({
        NSString * num = self.detailModel.num;
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(self.numControl.left + 15, header.height, UIScreenWidth - 30, 40)
                                                         text:[NSString stringWithFormat:@"当前只剩%@间",num]
                                                    textColor:SecendTextColor
                                                         font:13
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [header addSubview:titleLable];
        titleLable.attributedText = [titleLable.text st_convertAttributeStringWithKeyWord:num attributes:@{NSForegroundColorAttributeName:BL_BlueBackGroundColor}];
        header.height = titleLable.bottom;
    });
    ({
        self.dayControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 0 + header.height, UIScreenWidth - 30, 50) leftTitle:@"居住天数" placeHolder:@"请输入居住天数"];
        [header addSubview:self.dayControl];
        self.dayControl.textFiled.keyboardType = UIKeyboardTypeNumberPad;
        header.height = self.dayControl.bottom;
    });
    ({
        self.nameControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + header.height, UIScreenWidth - 30, 50) leftTitle:@"入住人" placeHolder:@"请输入住人姓名"];
        [header addSubview:self.nameControl];
        header.height = self.nameControl.bottom;
    });
    
    ({
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(self.numControl.left + 15, header.height, UIScreenWidth - 30, 40)
                                                         text:@"请确保填写的入住人姓名与实际入住人证件上的姓名一致"
                                                    textColor:SecendTextColor
                                                         font:13
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [header addSubview:titleLable];
        header.height = titleLable.bottom;
    });
    ({
        self.phoneControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 0 + header.height, UIScreenWidth - 30, 50) leftTitle:@"手机号" placeHolder:@"请输联系方式"];
        [header addSubview:self.phoneControl];
        header.height = self.phoneControl.bottom;
    });
    
    
    
    ({
        self.timeControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + header.height, UIScreenWidth - 30, 50) leftTitle:@"入住时间" placeHolder:@"请输入注时间"];
        [header addSubview:self.timeControl];
        header.height = self.timeControl.bottom + 40;
        self.timeControl.textFiled.font = [UIFont systemFontOfSize:12];
        self.timeControl.userInteractionEnabled = NO;
        NSDate * date = self.detailModel.dateArray.firstObject;
        self.timeControl.textFiled.text = [NSString stringWithFormat:@"%@,共一晚",date.st_yyyyMMddByLineWithDate];
    });
    
    
    
    
    self.tableView.tableHeaderView = header;
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 49)];
    
    STButton * priceButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth / 2, 49)
                                                       title:[NSString stringWithFormat:@"￥%@",self.detailModel.price]
                                                  titleColor:UIColor.redColor
                                                   titleFont:15
                                                cornerRadius:0
                                             backgroundColor:UIColor.whiteColor
                                             backgroundImage:nil
                                                       image:nil];
    priceButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    priceButton.top = TMUtils.tabbar.top;
    [self.view addSubview:priceButton];
    self.moneyButton = priceButton;
    __weak typeof(self) weakSelf =  self;
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake( UIScreenWidth / 2, 0, UIScreenWidth / 2, 49)
                                                     title:@"提交订单"
                                                titleColor:UIColor.whiteColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:BL_BlueBackGroundColor
                                           backgroundImage:nil
                                                     image:nil];
    buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    buyButton.top = TMUtils.tabbar.top;
    [buyButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedCommitOrderAction];
    }];
    [self.view addSubview:buyButton];
    [self addNotifacations];
}
#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(st_textFiledDidChange:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)st_textFiledDidChange:(NSNotification*)notifacation{
    
    if (notifacation.object == self.dayControl.textFiled) {
        NSInteger day = self.dayControl.textFiled.text.integerValue;
        NSDate * date = self.detailModel.dateArray.firstObject;
        NSString * dayString = date.st_yyyyMMddByLineWithDate;
        for (NSInteger i = 1 ; i < day; i ++) {
            date = [NSCalendar st_nextDayDate:date];
            dayString = [NSString stringWithFormat:@"%@,%@",dayString,date.st_yyyyMMddByLineWithDate];
        }
        self.timeControl.textFiled.text = [NSString stringWithFormat:@"%@,共%ld晚",dayString,day];
        NSString *price  = [NSString stringWithFormat:@"￥%0.2f",self.detailModel.price.floatValue * day];
        [self.moneyButton setTitle:price forState:UIControlStateNormal];
        
    }
    if (notifacation.object == self.numControl.textFiled) {
        NSInteger num = self.numControl.textFiled.text.integerValue;
        if (num > self.detailModel.num.integerValue) {
            self.numControl.textFiled.text = self.detailModel.num;
        }
    }
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --Action Method
- (void)onSelctedCommitOrderAction{
    if (!self.numControl.textFiled.text.length) {
        [SVProgressHUD showInfoWithStatus:self.numControl.textFiled.placeholder];
        return;
    }
    if (!self.dayControl.textFiled.text.length) {
        [SVProgressHUD showInfoWithStatus:self.dayControl.textFiled.placeholder];
        return;
    }
    if (self.dayControl.textFiled.text.integerValue < 1) {
        [SVProgressHUD showInfoWithStatus:self.dayControl.textFiled.placeholder];
        return;
    }
    if (!self.nameControl.textFiled.text.length) {
        [SVProgressHUD showInfoWithStatus:self.nameControl.textFiled.placeholder];
        return;
    }
    if (!self.phoneControl.textFiled.text.length) {
        [SVProgressHUD showInfoWithStatus:self.phoneControl.textFiled.placeholder];
        return;
    }
    [self sendHotleCommitOrdeRequest];
}
#pragma mark --NetWork Method
- (void)sendHotleCommitOrdeRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/api/hotel/reserve"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if (self.detailModel) {
        [dic setObject:self.detailModel.r_id forKey:@"r_id"];
        [dic setObject:self.numControl.textFiled.text forKey:@"num"];
        [dic setObject:self.nameControl.textFiled.text forKey:@"user_name"];
        [dic setObject:self.phoneControl.textFiled.text forKey:@"phone"];
        [dic setObject:self.dayControl.textFiled.text forKey:@"days"];
        NSDate * date = self.detailModel.dateArray.firstObject;
        [dic setObject:date.st_localDate.st_yyyyMMddhhmmssWithDate forKey:@"arrive"];
        
        NSInteger day = self.dayControl.textFiled.text.integerValue;
        for (NSInteger i = 1 ; i < day; i ++) {
            date = [NSCalendar st_nextDayDate:date];
        }
        [dic setObject:date.st_localDate.st_yyyyMMddhhmmssWithDate forKey:@"leave"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 //订单号
                                                 [SVProgressHUD dismiss];
                                                 
                                                 NSString * data = [responseObject[@"data"] description];
                                                 if (data.length) {
                                                     BLHotlePayViewController * vc = BLHotlePayViewController.new;
                                                     vc.orderNum = data;
                                                     vc.moneySting = self.moneyButton.currentTitle;
                                                     vc.desString = [NSString stringWithFormat:@"商品:%@ * %@",self.detailModel.room_name,self.numControl.textFiled.text];
                                                     [self.navigationController pushViewController:vc animated:YES];
                                                 }else{
                                                     [SVProgressHUD showInfoWithStatus:@"生成订单失败"];
                                                 }

                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD showInfoWithStatus:@"生成订单失败"];
                                             }];
    
}
@end

